﻿using System.Collections.Generic;
using Kwan.Cache;
using Kwan.Data;
using BG.Data;
using System;
using BG.Service;

namespace BG.Web
{
    public class PriceManager
    {
        public PriceManager()
        {
            //theCache = CacheManage.Instance;
        }
        private class Shell
        {
            internal static readonly PriceManager instance = new PriceManager();
        }
        public static PriceManager Instance
        {
            get { return Shell.instance; }
        }
        /// <summary>
        /// 获取最终房价
        /// </summary>
        /// <param name="checkInDate">入住时间</param>
        /// <param name="checkOutDate">退房时间</param>
        /// <param name="roomPrice">房间正常价格</param>
        /// <param name="roomHalfPrice">半天价</param>
        /// <param name="roomNightPrice">夜房价</param>
        /// <returns>最终房价</returns>
        public double GetFinalPrice(DateTime checkInDate, DateTime checkOutDate, double roomPrice, double roomHalfPrice, double roomNightPrice)
        {
            TConfig config = ConfigManager.Instance.LoadFromCache();
            string virtualDate = "2011-11-15 ";
            TimeSpan cfNightTime = DateTime.Parse(virtualDate + config.NightTime).TimeOfDay;            //夜房时间
            TimeSpan cfNewDayTime = DateTime.Parse(virtualDate + config.NewDayTime).TimeOfDay;          //新的一天开始的时间
            TimeSpan cfHalfDayTime = DateTime.Parse(virtualDate + config.AddHalfTime).TimeOfDay;        //超时增加一天的时间

            TimeSpan cfCheckOutTime = DateTime.Parse(virtualDate + config.AddHourTime).AddMinutes(config.AllowTime).TimeOfDay;       //默认退房时间
            double finalRoomPrice = 0;

            TimeSpan checkInTime = checkInDate.TimeOfDay;
            TimeSpan checkOutTime = checkOutDate.TimeOfDay;

            double finalPrice = 0;

            int liveDays = (checkOutDate.Date - checkInDate.Date).Days;
            int outHours = Convert.ToInt32((checkOutTime - cfCheckOutTime).TotalHours);   //所超时间
            double liveHours = (checkOutDate - checkInDate).TotalHours;

            if (roomHalfPrice == 0.00 || roomNightPrice == 0.00)    //入住时指定了价格
            {
                finalRoomPrice = roomPrice;
            }
            else if (checkInTime >= cfNightTime && checkInTime <= cfNewDayTime)   //00:00-6:00 入住
            {
                finalRoomPrice = roomNightPrice;
            }
            else if (checkInTime > cfNewDayTime && checkInTime <= cfHalfDayTime)  //6:00-18:00 入住
            {
                finalRoomPrice = roomPrice;
            }
            else        //18:00-00:00 入住
            {
                finalRoomPrice = roomHalfPrice;
            }

            //计算超时及天数
            if (checkOutTime <= cfCheckOutTime || liveHours < 4)    //没有超时
            {
                finalPrice += finalRoomPrice;
            }
            else
            {
                if (outHours > 3)
                    finalRoomPrice = roomHalfPrice;

                finalPrice += finalRoomPrice + config.OutTimePrice * outHours;
            }
            if (liveDays >= 2)
                finalPrice += roomPrice * (liveDays - 1);
            return finalPrice;
        }

        public double GetHourPrice(DateTime checkInDate, DateTime checkOutDate, double roomPrice, double roomHalfPrice, double roomHourPrice)
        {
            TConfig config = ConfigManager.Instance.LoadFromCache();
            string virtualDate = "2011-11-15 ";
            TimeSpan cfNightTime = DateTime.Parse(virtualDate + config.NightTime).TimeOfDay;            //夜房时间
            TimeSpan cfNewDayTime = DateTime.Parse(virtualDate + config.NewDayTime).TimeOfDay;          //新的一天开始的时间
            TimeSpan cfHalfDayTime = DateTime.Parse(virtualDate + config.AddHalfTime).TimeOfDay;        //超时增加一天的时间

            TimeSpan cfCheckOutTime = DateTime.Parse(virtualDate + config.AddHourTime).AddMinutes(config.AllowTime).TimeOfDay;       //默认退房时间
            double finalRoomPrice = 0;

            TimeSpan checkInTime = checkInDate.TimeOfDay;
            TimeSpan checkOutTime = checkOutDate.TimeOfDay;

            double finalPrice = 0;

            int liveDays = (checkOutDate.Date - checkInDate.Date).Days;
            //int outHours = Convert.ToInt32((checkOutTime - checkInTime).TotalHours);   //所超时间
            double liveMinutes = (checkOutDate - checkInDate).TotalMinutes;

            if (liveMinutes <= (180 + config.AllowTime))    //没有超时
            {
                finalPrice = roomHourPrice;
            }
            else if (liveMinutes > (180 + config.AllowTime) && liveMinutes <= (360 + config.AllowTime)) //未超3小时
            {
                finalRoomPrice = roomHourPrice;
                double outHours = Math.Ceiling((double)(liveMinutes - 180 - config.AllowTime) / 60);
                finalPrice += finalRoomPrice + config.OutTimePrice * outHours;
            }
            else
            {
                finalRoomPrice = roomHalfPrice;
                finalPrice += finalRoomPrice + config.OutTimePrice * ((180 + config.AllowTime) / 60);
            }

            if (liveDays >= 2)
                finalPrice += roomPrice * (liveDays - 1);
            return finalPrice;
        }
        
        #region IDisposable成员
        public void Dispose()
        {
            Dispose(true);
            //GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                lock (this)
                {
                    if (disposing)
                    {
                        //config = null;
                        //user = null;
                    }
                    m_disposed = true;
                }
            }
        }
        ~PriceManager()
        {
            Dispose(false);
        }
        private bool m_disposed;
        #endregion
    }
}
